﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Drawing;

public partial class giaovien_TrangKetQuaHocTap : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsLoadCombobox cls = new clsLoadCombobox();
    clsThaoTac thaotac = new clsThaoTac();
    clsKiemTraTrangThai clskttt = new clsKiemTraTrangThai();
    string manam;
    string malop;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            manam = Request.QueryString["MaNam"];
            malop = Request.QueryString["MaLop"];
            if (!IsPostBack)
            {
                string magv = Session["Username"].ToString();
                cls.LoadComboboxNamHoc(drNamHoc);
                cls.LoadComboboxLopHocTheoMaGV(drLopHoc, int.Parse(drNamHoc.SelectedValue.ToString()),magv);
                if (manam != "" && malop != "" && manam != null && malop != null)

                    drNamHoc.SelectedValue = manam;
                drLopHoc.SelectedValue = malop;
                HienThi();
               
            }
        }
    }
    DataTable dtk;
    DataTable dtk1;
    protected void drNamHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        cls.LoadComboboxLopHoc(drLopHoc, int.Parse(drNamHoc.SelectedValue.ToString()));
    }
    void kiemtratrangthai()
    {
        if (lblTrangThai.Text == "Đang chờ duyệt")
        {
            
            btnGuiBangDiem.Visible = false;
            btnHuy.Visible = true;

        }

        // grvNhapDiem.Enabled = true;
        if (lblTrangThai.Text == "Cần sửa lại bảng kết quả")
        {
            
            btnGuiBangDiem.Visible = false;
            btnHuy.Visible = false;
        }

        if (lblTrangThai.Text == "Đã duyệt")
        {
            
            btnGuiBangDiem.Visible = false;
            btnHuy.Visible = false;

        }
        if (lblTrangThai.Text == "Cần sửa lại bảng kết quả")
        {
           
            btnGuiBangDiem.Visible = false;
            btnHuy.Visible = false;

        }
        
        if (lblTrangThai.Text == "Chưa gửi bảng điểm")
        {
            
            btnGuiBangDiem.Visible = true;
            btnHuy.Visible = false;
            lblNgayGui.Text = "";

        }
    }
    public void HienThi()
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        //int maky=0;
        if (rdoKyI.Checked == true)
        {
            lblTrangThai.Text = clskttt.trangthaibangdiemcualophocky(manam, malop, 1);
            lblThongBao.Text = clskttt.HienThiThongBaoBDHK(manam, malop, 1);
           DataTable dt= DiemTongKetCacMon(manam, 1, malop);
           grvBangDiemTK.DataSource = dt;
           grvBangDiemTK.DataBind();
           thaotac.SoLuongXepLoaiHocLucKy(manam, malop, 1, lblGioi, lblKha, lblTrungBinh, lblYe1, lblKem);
            
        }
        if (rdoKyII.Checked == true)
        {
            lblTrangThai.Text = clskttt.trangthaibangdiemcualophocky(manam, malop, 2);
            lblThongBao.Text = clskttt.HienThiThongBaoBDHK(manam, malop, 2);
           DataTable dt1= DiemTongKetCacMon(manam, 2, malop);
           grvBangDiemTK.DataSource = dt1;
           grvBangDiemTK.DataBind();
           thaotac.SoLuongXepLoaiHocLucKy(manam, malop, 2, lblGioi, lblKha, lblTrungBinh, lblYe1, lblKem);
            
        }
        if (rdoCaNam.Checked == true)
        {
           DataTable dt2= DiemTKCaNam(manam, malop);
           grvBangDiemTK.DataSource = dt2;
           grvBangDiemTK.DataBind();
        }
        kiemtratrangthai();
    }
    protected void btnHienThi_Click(object sender, EventArgs e)
    {

        HienThi();
    }
    
    public  DataTable  DiemTongKetCacMon(int manam, int maky, int malop)
    {
        int stt = 0;
        dtk = new DataTable();
        dtk.Columns.Add("STT", typeof(string));
        dtk.Columns.Add("MaHS", typeof(string));
        dtk.Columns.Add("TenHS", typeof(string));
        dtk.Columns.Add("Toan", typeof(string));
        dtk.Columns.Add("Ly", typeof(string));
        dtk.Columns.Add("Hoa", typeof(string));
        dtk.Columns.Add("Sinh", typeof(string));
        dtk.Columns.Add("Tin", typeof(string));
        dtk.Columns.Add("Van", typeof(string));
        dtk.Columns.Add("LichSu", typeof(string));
        dtk.Columns.Add("DiaLy", typeof(string));
        dtk.Columns.Add("NgoaiNgu", typeof(string));
        dtk.Columns.Add("GDCD", typeof(string));
        dtk.Columns.Add("CongNghe", typeof(string));
        dtk.Columns.Add("TheDuc", typeof(string));
        dtk.Columns.Add("GDQP", typeof(string));
        dtk.Columns.Add("DTB", typeof(string));
        dtk.Columns.Add("HocLuc", typeof(string));
        dtk.Columns.Add("HanhKiem", typeof(string));
        dtk.Columns.Add("NghiCP", typeof(string));
        dtk.Columns.Add("NghiKP", typeof(string));
        dtk.Columns.Add("DanhHieu", typeof(string));

        var c = from p in db.funcLayMaHSTrongBangDiem(manam, maky, malop) select p;
        if (c.Count() != 0)
        {
            foreach (var con in c)
            {
                stt = stt + 1;
                DataRow row = dtk.NewRow();
                row[0] = stt.ToString();
                row[1] = con.MaHS;
                row[2] = con.TenHS;
                var d1 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 1, row[1].ToString()) select p.DTBMonHocKy;
                if (d1.Count() != 0)
                {
                    row[3] = d1.First().ToString();

                }
                else { row[3] = ""; }

                var d2 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 2, con.MaHS.ToString()) select p.DTBMonHocKy;
                if (d2.Count() != 0)
                { row[4] = d2.First().ToString(); }
                else { row[4] = ""; }

                var d3 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 3, con.MaHS.ToString()) select p.DTBMonHocKy;
                if (d3.Count() != 0)
                { row[5] = d3.First().ToString(); }
                else { row[5] = ""; }

                var d4 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 4, con.MaHS.ToString()) select p.DTBMonHocKy;
                if (d4.Count() != 0)
                { row[6] = d4.First().ToString(); }
                else { row[6] = ""; }

                var d5 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 5, con.MaHS.ToString()) select p.DTBMonHocKy;
                if (d5.Count() != 0)
                { row[7] = d5.First().ToString(); }
                else { row[7] = ""; }

                var d6 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 6, con.MaHS.ToString()) select p.DTBMonHocKy;
                if (d6.Count() != 0)
                { row[8] = d6.First().ToString(); }
                else { row[8] = ""; }

                var d7 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 7, con.MaHS) select p.DTBMonHocKy;
                if (d7.Count() != 0)
                { row[9] = d7.First().ToString(); }
                else { row[9] = ""; }

                var d8 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 8, con.MaHS) select p.DTBMonHocKy;
                if (d8.Count() != 0)
                { row[10] = d8.First().ToString(); }
                else { row[10] = ""; }

                var d9 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 9, con.MaHS) select p.DTBMonHocKy;
                if (d9.Count() != 0)
                { row[11] = d9.First().ToString(); }
                else { row[11] = ""; }

                var d10 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 10, con.MaHS) select p.DTBMonHocKy;
                if (d10.Count() != 0)
                { row[12] = d10.First().ToString(); }
                else { row[12] = ""; }

                var d11 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 11, con.MaHS) select p.DTBMonHocKy;
                if (d11.Count() != 0)
                { row[13] = d11.First().ToString(); }
                else { row[13] = ""; }

                var d12 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 12, con.MaHS) select p.DTBMonHocKy;
                if (d12.Count() != 0)
                { row[14] = d12.First().ToString(); }
                else { row[14] = ""; }

                var d13 = from p in db.funcTraVeDTBCuaTungMonHoc(manam, maky, 13, con.MaHS) select p.DTBMonHocKy;
                if (d13.Count() != 0)
                { row[15] = d13.First().ToString(); }
                else { row[15] = ""; }

                var dtb = from p in db.funcTraVeDTBChungCuaHocKy(manam, maky, con.MaHS) select p;
                if (dtb.Count() != 0)
                {
                    if (dtb.First().DiemTongKet == null)
                    {
                        row[16] = ""; row[18] = "";
                    }
                    if (dtb.First().DiemTongKet != null)
                    {
                        row[16] = dtb.First().DiemTongKet.ToString();
                        if (dtb.First().HanhKiem == null)
                        {

                            row[18] = "";
                        }
                        if (dtb.First().HanhKiem != null)
                        { row[18] = dtb.First().HanhKiem.ToString(); }
                    }
                }
                else { row[16] = ""; row[18] = ""; }
                string dtoan1 = row[3].ToString();
                string dvan1 = row[8].ToString();
                float dtb1 = float.Parse(row[16].ToString());
                row[17] =thaotac.DanhGiaHocLuc(dtb1, dtoan1, dvan1, row);
                db.procCapNhatHocLuc(manam, maky, con.MaHS, row[17].ToString());
                int nghicp =int.Parse(db.funcTraVeNghiCP(manam,maky,con.MaHS,1).ToString());
                int nghikp = int.Parse(db.funcTraVeNghiCP(manam, maky, con.MaHS, 2).ToString());   
                row[19] = nghicp.ToString();
                row[20] =nghikp.ToString();
                    row[21] = thaotac.DanhGiaKhenThuong(row[17].ToString(),row[18].ToString());
                    dtk.Rows.Add(row);

                }
                //grvBangDiemTK.DataSource = dtk;
                //grvBangDiemTK.DataBind();
            }
        return dtk;
        //thaotac.SoLuongXepLoaiHocLucKy(manam, malop, maky, lblGioi, lblKha, lblTrungBinh, lblYe1, lblKem);
        }
    
    private DataTable DiemTKCaNam(int manam, int malop)
    {
        int stt = 0;
        dtk1 = new DataTable();
        dtk1.Columns.Add("STT", typeof(string));
        dtk1.Columns.Add("MaHS", typeof(string));
        dtk1.Columns.Add("TenHS", typeof(string));
        dtk1.Columns.Add("Toan", typeof(string));
        dtk1.Columns.Add("Ly", typeof(string));
        dtk1.Columns.Add("Hoa", typeof(string));
        dtk1.Columns.Add("Sinh", typeof(string));
        dtk1.Columns.Add("Tin", typeof(string));
        dtk1.Columns.Add("Van", typeof(string));
        dtk1.Columns.Add("LichSu", typeof(string));
        dtk1.Columns.Add("DiaLy", typeof(string));
        dtk1.Columns.Add("NgoaiNgu", typeof(string));
        dtk1.Columns.Add("GDCD", typeof(string));
        dtk1.Columns.Add("CongNghe", typeof(string));
        dtk1.Columns.Add("TheDuc", typeof(string));
        dtk1.Columns.Add("GDQP", typeof(string));
        dtk1.Columns.Add("DTB", typeof(string));
        dtk1.Columns.Add("HocLuc", typeof(string));
        dtk1.Columns.Add("HanhKiem", typeof(string));
        dtk1.Columns.Add("NghiCP", typeof(string));
        dtk1.Columns.Add("NghiKP", typeof(string));
        dtk1.Columns.Add("DanhHieu", typeof(string));
        var c = from p in db.funcLayMaHSTrongBangDiem(manam, 1, malop) select p;
        var c1 = from p in db.funcLayMaHSTrongBangDiem(manam, 2, malop) select p;
        if (c.Count() != 0)
        {
            if (c1.Count() != 0)
            {
                foreach (var con in c)
                {
                    stt = stt + 1;
                    float tongdiem = 0;
                    int tongheso = 0;
                    DataRow row = dtk1.NewRow();
                    row[0] = stt.ToString();
                    row[1] = con.MaHS;
                    row[2] = con.TenHS;
                    row[3] =thaotac.tmptinhDiemTBTCN(manam, 1, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[4] =thaotac.tmptinhDiemTBTCN(manam, 2, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[5] =thaotac.tmptinhDiemTBTCN(manam, 3, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[6] =thaotac.tmptinhDiemTBTCN(manam, 4, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[7] = thaotac.tmptinhDiemTBTCN(manam, 5, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[8] = thaotac.tmptinhDiemTBTCN(manam, 6, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[9] =thaotac.tmptinhDiemTBTCN(manam, 7, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[10] =thaotac.tmptinhDiemTBTCN(manam, 8, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[11] =thaotac.tmptinhDiemTBTCN(manam, 9, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[12] =thaotac.tmptinhDiemTBTCN(manam, 10, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[13] =thaotac.tmptinhDiemTBTCN(manam, 11, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[14] =thaotac.tmptinhDiemTBTCN(manam, 12, row[1].ToString(), ref tongdiem, ref tongheso);
                    row[15] =thaotac.tmptinhDiemTBTCN(manam, 13, row[1].ToString(), ref tongdiem, ref tongheso);
                    string dtb1 = thaotac.LamTronDiem((tongdiem / tongheso).ToString());
                    row[16] = dtb1;
                    var hk = from p in db.funcTraVeDTBChungCuaHocKy(manam, 2, con.MaHS) select p;
                    string dtoan1 =row[3].ToString();
                    string dvan1=row[8].ToString();
                    float  dtb=float.Parse(row[16].ToString());
                    row[17] =thaotac.DanhGiaHocLuc(dtb, dtoan1, dvan1, row);
                    int nghicp1=int.Parse(db.funcTraVeNghiCP(manam,1,row[1].ToString(),1).ToString());
                    int nghicp2=int.Parse(db.funcTraVeNghiCP(manam,2,row[1].ToString(),1).ToString());
                    int nghikp1 = int.Parse(db.funcTraVeNghiCP(manam, 1, row[1].ToString(), 2).ToString());
                    int nghikp2 = int.Parse(db.funcTraVeNghiCP(manam, 2, row[1].ToString(), 2).ToString());     
                    row[18] = "";
                    row[19] = (nghicp1+nghicp2).ToString();
                    row[20] = (nghikp1+nghikp2).ToString();
                    row[21] = thaotac.DanhGiaKhenThuong(row[17].ToString(), row[18].ToString());
                    dtk1.Rows.Add(row);
                    db.procLuuHSVaoBangCaNam(manam, con.MaHS,double.Parse(row[16].ToString()), row[17].ToString());

                  }
                    //grvBangDiemTK.DataSource = dtk;
                    //grvBangDiemTK.DataBind();
                }
                if (c1.Count() == 0)
                {
                    
                    foreach (var con in c)
                    {
                        stt = stt + 1;
                        DataRow row = dtk1.NewRow();
                        row[0] = stt.ToString();
                        row[1] = con.MaHS;
                        row[2] = con.TenHS;
                        row[3] = ""; row[4] = ""; row[5] = ""; row[6] = ""; row[7] = "";
                        row[8] = ""; row[9] = ""; row[10] = ""; row[11] = "";
                        row[12] = ""; row[13] = ""; row[14] = ""; row[15] = ""; row[16] = "";
                        row[17] = "";
                        row[18] = "";
                        row[19] = "";
                        row[20] = ""; row[21] = "";
                        dtk1.Rows.Add(row);
                    }
                    //grvBangDiemTK.DataSource = dtk;
                    //grvBangDiemTK.DataBind();
                }
            }
        return dtk1;
        //thaotac.SoLuongXepLoaiCaNam(manam, malop, lblGioi, lblKha, lblTrungBinh, lblYe1, lblKem);
        }

    protected void grvBangDiemTK_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 1; i <= 14; i++)
            {
                Label lbl = (Label)e.Row.FindControl("lbl"+i.ToString());
                KiemTraDiemKem(lbl);
            }
        }
    }
    void KiemTraDiemKem(Label txt)
    {
        if (txt.Text != "")
        {
            float d = float.Parse(txt.Text);
            if (d < 3.5)
            {
                txt.ForeColor = Color.Red;
            }
        }
    }
    
    protected void drLopHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        HienThi();
    }
    protected void rdoKyI_CheckedChanged(object sender, EventArgs e)
    {
        HienThi();
    }
    protected void rdoKyII_CheckedChanged(object sender, EventArgs e)
    {
        HienThi();
    }
    protected void rdoCaNam_CheckedChanged(object sender, EventArgs e)
    {
        HienThi();
    }
    protected void btnGuiBangDiem_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int maky=1;
        if(rdoKyI.Checked==true)
        {
            maky=1;
        }
        if(rdoKyII.Checked==true)
        {
            maky=2;
        }
        if (clskttt.kiemtrabdhk(manam, malop, maky)==false)
        {
            TrangThaiBangDiemTheoKy bdhk = new TrangThaiBangDiemTheoKy();
            bdhk.MaNam = manam;
            bdhk.MaLop = malop;
            bdhk.MaKy = maky;
            bdhk.TrangThaiChoDuyet = true;
            bdhk.TrangThaiHoanThanh = false;
            bdhk.NgayGui = DateTime.Now.Date;
            db.TrangThaiBangDiemTheoKies.InsertOnSubmit(bdhk);
            db.SubmitChanges();
            lblTrangThai.Text = clskttt.trangthaibangdiemcualophocky(manam, malop, maky);
            clskttt.NgayGuiBDHK(manam, malop, maky, lblNgayGui);
            lblNgayDuyet.Text = "";
        }
        if (clskttt.kiemtrabdhk(manam, malop, maky) == true)
        {
            TrangThaiBangDiemTheoKy bdhk = db.TrangThaiBangDiemTheoKies.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam);
            bdhk.TrangThaiChoDuyet = true;
            bdhk.TrangThaiHoanThanh = false;
            bdhk.NgayGui = DateTime.Now.Date;
            bdhk.DaSua = true;
            db.SubmitChanges();
            lblTrangThai.Text = clskttt.trangthaibangdiemcualophocky(manam, malop, maky);
            clskttt.NgayGuiBDHK(manam, malop, maky, lblNgayGui);
            lblNgayDuyet.Text = "";
        }
        kiemtratrangthai();

    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int maky = 1;
        if (rdoKyI.Checked == true)
        {
            maky = 1;
        }
        if (rdoKyII.Checked == true)
        {
            maky = 2;
        }
        TrangThaiBangDiemTheoKy bdhk = db.TrangThaiBangDiemTheoKies.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam);
        bdhk.TrangThaiChoDuyet = false;
        bdhk.TrangThaiHoanThanh = false;
        
        bdhk.DaSua = true;
        db.SubmitChanges();
        lblTrangThai.Text = clskttt.trangthaibangdiemcualophocky(manam, malop, maky);
        clskttt.NgayGuiBDHK(manam, malop, maky, lblNgayGui);
        lblNgayDuyet.Text = "";
        kiemtratrangthai();
    }
    protected void btnXuatExcel_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        if (rdoKyI.Checked == true)
        {
            DataTable dt = DiemTongKetCacMon(manam, 1, malop);
           thaotac.Export1(dt, "TK " + drLopHoc.SelectedItem.Text, "BẢNG ĐIỂM TỔNG KẾT LỚP " + drLopHoc.SelectedItem.Text + "NĂM HỌC " + drNamHoc.SelectedItem.Text + "HOC KỲ 1");
        }
        if (rdoKyII.Checked == true)
        {
            DataTable dt1 = DiemTongKetCacMon(manam, 2, malop);
            thaotac.Export1(dt1, "TK " + drLopHoc.SelectedItem.Text, "BẢNG ĐIỂM TỔNG KẾT LỚP " + drLopHoc.SelectedItem.Text + "NĂM HỌC " + drNamHoc.SelectedItem.Text + "HOC KỲ 2");
        }
        if (rdoCaNam.Checked == true)
        {
            DataTable dt2 = DiemTKCaNam(manam, malop);
            thaotac.Export1(dt2, "TK " + drLopHoc.SelectedItem.Text, "BẢNG ĐIỂM TỔNG KẾT LỚP " + drLopHoc.SelectedItem.Text + "  NĂM HỌC " + drNamHoc.SelectedItem.Text + "CẢ NĂM");
        }
    }
    
}
